package edu.umn.actvs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.StringTokenizer;

import android.util.Log;
import edu.umn.ftpmaster.Command;

public class CommandHelper {
	private static final String LOC_URL = "";
	private static final String CHARSET = "";

	public static Command sendCommand(String ipAddress, int port, Command cmd) throws ClassNotFoundException{
		//String ipAddress = "10.20.45.72";
		//int port = 5000;
		Command rescmd = null;
//		StringTokenizer st = new StringTokenizer(ipAddress, ".");
//		byte[] ip = new byte[4];
//		int i = 0;
//		while (st.hasMoreTokens()) {
//			ip[i++] = (Byte)Byte.parseByte(st.nextToken());
//		}
		InetAddress addr = null;
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
			addr = InetAddress.getByName(ipAddress);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Socket sock = null;
		try {
			sock = new Socket(addr,port);
			oos = new ObjectOutputStream(sock.getOutputStream());
			ois = new ObjectInputStream(sock.getInputStream()); 
			oos.writeObject(cmd);
			rescmd = (Command)ois.readObject();
			Log.w("Logging:", rescmd.getOption());
			//cmd = new Command("search_loc",);
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rescmd;
	}

	public static long convertTime(String year,
			String month, String date,
			String hour,String minutes, String period) throws ParseException{
		Calendar c = Calendar.getInstance();
		long res=0;

		int hr = Integer.parseInt(hour);
		int min = Integer.parseInt(minutes);

		if(period.equals("PM")&& hr != 12){
			hr = (hr+12)%24;
		} else if(period.equals("AM")&& hr == 12){
			hr -= 12;
		}
		c.set(Integer.parseInt(year), Integer.parseInt(month),Integer.parseInt(date),
				hr, min, 0);

		res = c.getTimeInMillis();



		return res;
	}

	public static String sendGetRequest(String requestParameters){
		String res = null;
		boolean flat;
		boolean flng;
		// Send a GET request to the servlet
		try{
			StringBuffer data = new StringBuffer();
			String urlStr = "http://maps.googleapis.com/maps/api/geocode/xml";
			
	
			
			if (requestParameters != null && requestParameters.length () > 0)
			{
				urlStr += "?" + requestParameters;
			}
			
	//		urlStr = "http://maps.googleapis.com/maps/api/geocode/xml?address=200+Union+Street,+Minneapolis+,+MN&sensor=true";
			
//			System.out.println(urlStr);
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection ();
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			char [] buff = new char[100];
			char [] buff1 = new char[100];
			while ((line = rd.readLine()  ) != null){
				if(line.contains("lat")){
					line = line.trim();
					line.getChars(5,line.length()-6,buff,0);
				}

				if(line.contains("lng")){	
					line = line.trim();
					line.getChars(5,line.length()-6,buff1,0);

					res  = (new String(buff))+":"+(new String(buff1));
				}
			}
			rd.close();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return res;
	}

}
